<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page isELIgnored="false" %>

<html>
<head>
    <title>书籍列表</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        /* 新增导航栏样式 */
        .navbar {
            background-color: #f8f9fa;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: #333;
            text-decoration: none;
        }

        .nav-links {
            display: flex;
            gap: 20px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            gap: 5px;
            color: #333;
            text-decoration: none;
            font-size: 1rem;
            padding: 5px 10px;
            border-radius: 4px;
            transition: all 0.3s ease;
        }

        .nav-link:hover {
            background-color: #e9ecef;
            color: #007bff;
        }

        .nav-link i {
            font-size: 1.2rem;
        }

        /* 原有样式保持不变 */
        .book-card {
            cursor: pointer;
            transition: transform 0.2s;
        }
        .book-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
        }
        .book-list {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            padding: 20px;
        }
        .book-card {
            width: 200px;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 10px;
            text-align: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .book-image {
            max-width: 100%;
            height: 150px;
            object-fit: cover;
            border-radius: 5px;
        }
        .book-info {
            margin-top: 10px;
            font-family: Arial, sans-serif;
        }
        .book-title {
            font-weight: bold;
            margin: 5px 0;
        }
        .action-buttons {
            margin-top: 10px;
            display: flex;
            justify-content: space-between;
        }
        .action-button {
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            transition: background-color 0.3s;
        }
        .favorite-btn {
            background-color: #ffdd59;
            color: #333;
        }
        .favorite-btn:hover {
            background-color: #ffd32a;
        }
        .cart-btn {
            background-color: #4bcffa;
            color: white;
        }
        .cart-btn:hover {
            background-color: #0fbcf9;
        }
        .buy-btn {
            background-color: #05c46b;
            color: white;
        }
        .buy-btn:hover {
            background-color: #04944b;
        }
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 10px;
        }
        .page-link {
            padding: 5px 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-decoration: none;
            color: #333;
        }
        .page-link:hover {
            background-color: #f1f1f1;
        }
        .current-page {
            background-color: #007bff;
            color: white;
            border-color: #007bff;
        }
        .search-container {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }
        .search-box {
            position: relative;
            width: 50%;
        }
        .search-input {
            width: 100%;
            padding: 10px 15px 10px 40px;
            border: 1px solid #ddd;
            border-radius: 25px;
            font-size: 1rem;
            outline: none;
            transition: all 0.3s;
        }
        .search-input:focus {
            border-color: #007bff;
            box-shadow: 0 0 5px rgba(0,123,255,0.3);
        }
        .search-icon {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #777;
        }
    </style>
</head>
<body>
<!-- 新增导航栏 -->
<nav class="navbar">
    <a href="${pageContext.request.contextPath}/student/home" class="logo">图书商城</a>
    <div class="nav-links">
        <a href="${pageContext.request.contextPath}/student/update/${user.uid}" class="nav-link">
            <i class="fas fa-user"></i>
            <span>个人中心</span>
        </a>
        <a href="${pageContext.request.contextPath}/orders/list" class="nav-link">
            <i class="fas fa-file-alt"></i>
            <span>订单管理</span>
        </a>
        <a href="${pageContext.request.contextPath}/cart/list" class="nav-link">
            <i class="fas fa-shopping-cart"></i>
            <span>购物车</span>
        </a>
        <a href="${pageContext.request.contextPath}/collection/list" class="nav-link">
            <i class="fas fa-heart"></i>
            <span>我的收藏</span>
        </a>
        <a href="${pageContext.request.contextPath}/history/list" class="nav-link">
            <i class="fas fa-heart"></i>
            <span>历史浏览</span>
        </a>
    </div>
</nav>

<!-- 搜索框 -->
<div class="search-container">
    <div class="search-box">
        <i class="fas fa-search search-icon"></i>
        <input type="text" class="search-input" placeholder="搜索商品...">
    </div>
</div>

<h1 style="text-align: center;">书籍列表</h1>
<div class="book-list">
    <c:forEach items="${goods}" var="goods">
        <div class="book-card">
            <img class="book-image" src="${goods.img}" alt="${goods.gname}封面" onclick="window.location.href='${pageContext.request.contextPath}/student/detail?gid=${goods.gid}'">
            <div class="book-info">
                <div class="book-title" onclick="window.location.href='${pageContext.request.contextPath}/student/detail?gid=${goods.gid}'">${goods.gname}</div>
                <div>作者：${goods.author}</div>
                <div>类型：${goods.type}</div>
                <div class="action-buttons">
                    <button class="action-button favorite-btn" onclick="window.location.href='${pageContext.request.contextPath}/collection/add?gid=${goods.gid}'">收藏</button>
                    <button class="action-button cart-btn" onclick="window.location.href='${pageContext.request.contextPath}/cart/add?gid=${goods.gid}'">购物车</button>
                    <button class="action-button buy-btn"
                            onclick="window.location.href='${pageContext.request.contextPath}/orders/add?gid=${goods.gid}'">
                        购买
                    </button>
                    <button class="action-button buy-btn"
                            onclick="window.location.href='${pageContext.request.contextPath}/orders/exchange?gid=${goods.gid}&name=${goods.gname}&price=${goods.price}&scorerequired=${goods.scorerequired}&img=${goods.img}&score=${currentUser.score}'">
                        兑换
                    </button>
                </div>
            </div>
        </div>
    </c:forEach>
</div>

<div class="pagination">
    <c:if test="${currentPage > 1}">
        <a href="${pageContext.request.contextPath}/goods/findGoodsByPage?page=${currentPage-1}" class="page-link">上一页</a>
    </c:if>
    <c:if test="${currentPage > 2}">
        <a href="${pageContext.request.contextPath}/goods/findGoodsByPage?page=${currentPage-2}" class="page-link">${currentPage-2}</a>
    </c:if>
    <c:if test="${currentPage > 1}">
        <a href="${pageContext.request.contextPath}/goods/findGoodsByPage?page=${currentPage-1}" class="page-link">${currentPage-1}</a>
    </c:if>
    <a href="${pageContext.request.contextPath}/goods/findGoodsByPage?page=${currentPage}" class="page-link current-page">${currentPage}</a>
    <c:if test="${totalPage > currentPage}">
        <a href="${pageContext.request.contextPath}/goods/findGoodsByPage?page=${currentPage+1}" class="page-link">${currentPage+1}</a>
    </c:if>
    <c:if test="${totalPage > currentPage+1}">
        <a href="${pageContext.request.contextPath}/goods/findGoodsByPage?page=${currentPage+2}" class="page-link">${currentPage+2}</a>
    </c:if>
    <c:if test="${totalPage > currentPage}">
        <a href="${pageContext.request.contextPath}/goods/findGoodsByPage?page=${currentPage+1}" class="page-link">下一页</a>
    </c:if>
</div>

<script>
    function addToFavorites(gid) {
        fetch('${pageContext.request.contextPath}/favorite/add?gid=' + gid, {
            method: 'POST'
        })
            .then(response => response.json())
            .then(data => {
                if(data.success) {
                    alert('已添加到收藏夹');
                } else {
                    alert(data.message || '添加收藏失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('操作失败，请重试');
            });
    }

    function addToCart(gid) {
        fetch('${pageContext.request.contextPath}/cart/add?gid=' + gid, {
            method: 'POST'
        })
            .then(response => response.json())
            .then(data => {
                if(data.success) {
                    alert('已添加到购物车');
                } else {
                    alert(data.message || '添加到购物车失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('操作失败，请重试');
            });
    }

    function buyNow(gid) {
        window.location.href = '${pageContext.request.contextPath}/order/checkout?gid=' + gid;
    }
</script>
</body>
</html>